package com.zhenwgang.listen_books.servlet.user;

import com.zhenwgang.listen_books.service.UserService;
import com.zhenwgang.listen_books.view_object.UserVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 注册动态资源
 * @author Violet
 * @date 2022/07/07 10:24
 **/

@WebServlet("/studio/user/register.do")
public class RegisterDoServlet extends HttpServlet {
    private final UserService userService = new UserService();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        System.out.println(username + "+" + password);

        UserVO userVO = userService.register(username, password);

        HttpSession session =  req.getSession();
        session.setAttribute("currentUser", userVO);

        resp.sendRedirect("/studio/");
    }
}